package com.sznaner.pwinputview;


import android.content.Context;
import android.os.Handler;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import java.util.ArrayList;
import java.util.List;

public class PWInputView extends LinearLayout {
    public TextView titleTV;
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;
    private ImageView delete,pwReset;
    private TextView other;
    private TextView one;
    private TextView two;
    private TextView three;
    private TextView four;
    private TextView five;
    private TextView six;
    private TextView seven;
    private TextView eight;
    private TextView nine;
    private TextView zero;
    private Boolean showPW = false;
    private List<String> input = new ArrayList<>();
    private int resetCount = 0;

    public PWInputView(final Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOrientation(VERTICAL);
        LayoutInflater.from(context).inflate(R.layout.pwinputview_layout, this);
        titleTV  = findViewById(R.id.titleTV);
        tv1 =  findViewById(R.id.tv1);
        tv2 = findViewById(R.id.tv2);
        tv3 = findViewById(R.id.tv3);
        tv4 = findViewById(R.id.tv4);
        one = findViewById(R.id.one);
        two = findViewById(R.id.two);
        three = findViewById(R.id.three);
        four = findViewById(R.id.four);
        five = findViewById(R.id.five);
        six = findViewById(R.id.six);
        seven = findViewById(R.id.seven);
        eight = findViewById(R.id.eight);
        nine = findViewById(R.id.nine);
        zero = findViewById(R.id.zero);
        other = findViewById(R.id.other);
        delete = (ImageView) findViewById(R.id.delete);
        pwReset = (ImageView) findViewById(R.id.pwReset);

        pwReset.setVisibility(INVISIBLE);
        titleTV.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                resetCount += 1;
                if(resetCount == 5){
                    Settings.System.putString(context.getContentResolver(), "com.sznaner.password","8888");
                    pwReset.setVisibility(VISIBLE);
                    resetCount = 0;
                    if(onResetListener != null) onResetListener.OnReset();
                    new  Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            pwReset.setVisibility(INVISIBLE);
                        }
                    },1500);
                }
            }
        });

        delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (input.size()>0){
                    remove(context);
                }else {
                    if (onClearCompleListener != null) {
                        onClearCompleListener.onClearCompled();
                    }
                }
            }
        });
        other.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
        one.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                add("1",context);
            }
        });
        two.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                add("2",context);
            }
        });
        three.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                add("3",context);
            }
        });
        four.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                add("4",context);
            }
        });
        five.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                add("5",context);
            }
        });
        six.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                add("6",context);
            }
        });
        seven.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                add("7",context);
            }
        });
        eight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                add("8",context);
            }
        });
        nine.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                add("9",context);
            }
        });
        zero.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                add("0",context);
            }
        });
    }

    public PWInputView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public PWInputView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public PWInputView(Context context) {
        super(context);

    }
    private void remove(Context context) {

        if (input.size() == 4) {
            input.remove(input.size() - 1);
            tv4.setText(null);
        } else if (input.size() == 3) {
            input.remove(input.size() - 1);
            tv3.setText(null);
        } else if (input.size() == 2) {
            input.remove(input.size() - 1);
            tv2.setText(null);
        } else if (input.size() == 1) {
            input.remove(input.size() - 1);
            tv1.setText(null);
        }
    }
    private void add(String pin, Context context){

        if(input.size()==0){
            input.add(pin);
            tv1.setText(pin);
            if(!showPW){
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tv1.setText("*");
                    }
                },500);
            }
        }else if(input.size()==1){
            input.add(pin);
            tv2.setText(pin);
            if(!showPW){
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tv2.setText("*");
                    }
                },500);
            }
        }else if(input.size()==2){
            input.add(pin);
            tv3.setText(pin);
            if(!showPW){
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tv3.setText("*");
                    }
                },500);
            }
        }else if (input.size() == 3){
            input.add(pin);
            tv4.setText(pin);
            if(!showPW){
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tv4.setText("*");
                    }
                },500);
            }
            StringBuilder stringBuilder = new StringBuilder();
            for (String str:input){
                stringBuilder.append(str);
            }
            if (onFullListener != null){
                onFullListener.OnFull(stringBuilder.toString());
            }
        }
    }
    OnClearCompleListener onClearCompleListener;
    OnFullListener onFullListener;
    OnResetListener onResetListener;
    public interface OnClearCompleListener{
        void onClearCompled();
    }
    public interface OnResetListener{
        void OnReset();
    }
    public interface OnFullListener{
        void OnFull(String vaule);
    }
    public void setOnFullListener(OnFullListener mListener){
        this.onFullListener = mListener;
    }

    public void setOnResetListener(OnResetListener onResetListener) {
        this.onResetListener = onResetListener;
    }

    public void setOnClearCompleListener(OnClearCompleListener mListener){
        this.onClearCompleListener = mListener;
    }

    public void clearAll(){
        input.clear();
        tv1.setText(null);
        tv2.setText(null);
        tv3.setText(null);
        tv4.setText(null);
    }
    public void setShowPW(Boolean showPW) {
        this.showPW = showPW;
    }

    public void setTitle(String title){
        titleTV.setText(title);
    }

    public void clearResetCount(){
        resetCount = 0;
    }
}
